import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:im_module/widget/OpenBigImagePage.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';

class ImageMessageWidget extends StatelessWidget {
  final V2TimMessage message;

  ImageMessageWidget(this.message);

  @override
  Widget build(BuildContext context) {
    /*message.imageElem?.imageList?.forEach((element) {
      print("--->${element?.toJson()}");
    });*/
    return ClipRRect(
      borderRadius: BorderRadius.circular(5),
      child: Container(
        width: 150,
        height: 200,
        child: Column(
          children: message.imageElem!.imageList!.map(
            (e) {
              if (e!.type == 2) {
                if (e.url != null) {
                  return GestureDetector(
                    onTap: () {
                      print("${e.toJson()}");
                      Navigator.push(context,
                          new MaterialPageRoute(builder: (ctx) {
                        return OpenBigImagePage("${e.url}",e.width!.toDouble(),e.height!.toDouble());
                      }));
                    },
                    child: CachedNetworkImage(
                      imageUrl: "${e.url}",
                      fit: BoxFit.cover,
                      width: 150,
                      height: 200,
                      placeholder: (context, url) => Container(
                        child: CupertinoActivityIndicator(radius: 15),
                      ),
                      errorWidget: (context, url, error) => Icon(Icons.error),
                    ),
                  );
                } else {
                  return Image.file(
                    new File(message.imageElem!.path!),
                    fit: BoxFit.cover,
                  );
                }
              } else {
                return SizedBox();
              }
            },
          ).toList(),
        ),
      ),
    );
  }
}
